package net.asrsmid.invoker.as;

import java.util.List;

/**
 * A DimensionsNotMatchException is thrown when the dimension or it length 
 * of table, headers, data don't match each other.
 * @author Chen WANG
 * @since 20 June 2011
 */
public class DimensionsNotMatchException extends TableException {
	private static final long serialVersionUID = 1L;

	/**
	 * Constructs a DimensionsNotMatchException with the given fields
	 * @param tableDim table's dimension
	 * @param headerDim table's headers' dimension
	 * @param dataDim table's data's dimension
	 */
	public DimensionsNotMatchException(int tableDim, int headerDim, int dataDim) {
		super(composeMsg(tableDim, headerDim, dataDim));
	}
	
	/**
	 * Constructs a DimensionsNotMatchException with the given fields
	 * @param tableDim an int array which means table's dimension and the length of each dimension
	 * @param headerDim an int array which means table's headers' dimension and the length of each dimension
	 */
	public DimensionsNotMatchException(int[] tableDim, int[][] headerDim) {
		super(composeMsg(tableDim, headerDim));
	}
	
	/**
	 * Constructs a DimensionsNotMatchException with the given fields
	 * @param tableDim an int array which means table's dimension and the length of each dimension
	 * @param dataDim an int array which means table's data's dimension and the length of each dimension
	 */
	public DimensionsNotMatchException(int[] tableDim, int[] dataDim) {
		super(composeMsg(tableDim, dataDim));
	}
	
	/**
	 * Constructs a DimensionsNotMatchException with the given fields
	 * @param tableDim an int array which means table's dimension and the length of each dimension
	 * @param dataDim an Integer array which means table's data's dimension and the length of each dimension
	 */
	public DimensionsNotMatchException(int[] tableDim, List<Integer> dataDim) {
		super(composeMsg(tableDim, dataDim));
	}
	
	private static String composeMsg(int tableDim, int headerDim, int dataDim) {
		StringBuilder sb = new StringBuilder("Dimensions don't match for constructing a Table object.");
		sb.append(" Table's dimension : ").append(tableDim);
		sb.append(", data's dimension : ").append(dataDim);
		sb.append(", headers' dimension : ").append(headerDim);
		return sb.toString(); 
	}
	
	private static String composeMsg(int[] tableDim, int[][] headerDim) {
		StringBuilder sb = new StringBuilder("Dimensions don't match for constructing a Table object.");
		sb.append(" Table's dimension : ").append(tableDim.toString());
		sb.append(", headers' dimension : ").append(headerDim.toString());
		return sb.toString(); 
	}
	
	private static String composeMsg(int[] tableDim, int[] dataDim) {
		StringBuilder sb = new StringBuilder("Dimensions don't match for constructing a Table object.");
		sb.append(" Table's dimension : ").append(tableDim.toString());
		sb.append(", data's dimension : ").append(dataDim.toString());
		return sb.toString(); 
	}
	
	private static String composeMsg(int[] tableDim, List<Integer> dataDim) {
		int[] iDataDim = new int[dataDim.size()];
		
		for (int i = 0; i < iDataDim.length; i++) {
			iDataDim[i] = dataDim.get(i);
		}
		return composeMsg(tableDim, dataDim);
	}
}
